package org.smarthings.things.commons.utils;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

public class QueryParametersUtils {

	public static final String OFFSET = "offset";
	public static final String LIMIT = "limit";
	
	public static final int DEFAULT_OFFSET = 0;
	public static final int DEFAULT_LIMIT = 20;

	public static int getOffset(UriInfo uri) {
		MultivaluedMap<String, String> queryParameters = uri.getQueryParameters();
		String offsetQueryParameter = queryParameters.getFirst(OFFSET);
		int offset = DEFAULT_OFFSET;
		if (offsetQueryParameter != null) {
			try {
				offset = Integer.parseInt(offsetQueryParameter);
			} catch (NumberFormatException nfe) {
				throw new IllegalArgumentException("Invalid offset " + offset);
			}
		}
		return offset;
	}

	public static int getLimit(UriInfo uri) {
		MultivaluedMap<String, String> queryParameters = uri.getQueryParameters();
		String limitQueryParameter = queryParameters.getFirst(LIMIT);
		int limit = DEFAULT_LIMIT;
		if (limitQueryParameter != null) {
			try {
				limit = Integer.parseInt(limitQueryParameter);
			} catch (NumberFormatException nfe) {
				throw new IllegalArgumentException("Invalid limit " + limit);
			}
		}
		return limit;
	}
	

}
